package com.shengyin.ec.controller.mail;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.shengyin.ec.exception.ControllerException;
import com.shengyin.ec.mail.MailSenderInfo;
import com.shengyin.ec.mail.SimpleMailSender;
import com.shengyin.ec.model.entity.Mail;
import com.shengyin.ec.service.mail.MailService;

@Controller
public class MailController {

	@Autowired
	MailService mailService;

	@RequestMapping("/toMailSender.do")
	public String toMailSender() {

		return null;
	}

	// 邮件发送的基本信息的设置
	@RequestMapping("/toSetMailSender.do")
	public String toSetMailSender() {

		return "redirect:/admin/mail/setMailSender.jsp";
	}

	// 注册内容的邮箱设置
	@RequestMapping("/toRegMailSender.do")
	public String toRegMailSender(Model model) {
		List<Mail> mailList=mailService.getMailList();
		model.addAttribute("mailList", mailList);
		return "/admin/mail/mailSend";
	}

	// 订购内容的邮箱设置
	@RequestMapping("/toBuyMailSender.do")
	public String toBuyMailSender(Model model) {
		List<Mail> mailList=mailService.getMailList();
		model.addAttribute("mailList", mailList);
		return "redirect:/admin/mail/setMailSender.jsp";
	}

	@RequestMapping("/toMailModify.do")
	public String toMailModify(@RequestParam("id") long id, Model model) {
		Mail mail = mailService.getMail(id);
		model.addAttribute("mail", mail);
		return "/admin/mail/mailModify";
	}

	@RequestMapping("/toMailList.do")
	public String toMailList(Model model, HttpServletRequest request) {

		int offset = 0;
		try {
			offset = Integer.valueOf(request.getParameter("pager.offset"));
			System.out.println("offset:" + offset);
		} catch (Exception ex) {
			System.out.println("初始化页数.......");
		}
		ArrayList<Mail> mailList = (ArrayList<Mail>) mailService.getMailList(
				offset, 10);// 默认10条才进行分页
		Long maxSize = mailService.getTotal();
		model.addAttribute("maxSize", maxSize);
		model.addAttribute("mailList", mailList);
		return "/admin/mail/mailList";
	}

	@RequestMapping("/mailAdd.do")
	public String createMail(MailForm mailForm){
		Mail mail= new Mail();
		try {
			BeanUtils.copyProperties(mail, mailForm);
			mailService.createMail(mail);
			return "redirect:/admin/mail/mailSendSucces.jsp";
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	
	@RequestMapping("/mailSend.do")
	public String createMail(MailContentForm mailForm) {

		try {
			Mail mail = mailService.getMail(mailForm.getId());
			mail.setContent(mailForm.getContent());
			mail.setToAddress(mailForm.getToAddress());
			mail.setSubject(mailForm.getSubject());
			
			MailSenderInfo mailInfo = new MailSenderInfo();

			BeanUtils.copyProperties(mailInfo, mail);
			SimpleMailSender sms = new SimpleMailSender();
			sms.sendHtmlMail(mailInfo);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		return "redirect:/admin/mail/mailSendSucces.jsp";

	}

	@RequestMapping("/mailDelete.do")
	public String deleteMail(@RequestParam("id") int id) {
		mailService.removeMailById(id);
		return "redirect:/toMailList.do";
	}

	@RequestMapping("/deleteSelectedMail.do")
	public String deleteSelectedMail(HttpServletRequest request)
			throws ControllerException {
		String[] ids = request.getParameterValues("ids");
		ArrayList<Long> idb = new ArrayList<Long>();
		if (ids == null)
			throw new ControllerException("所选新闻不存在");
		mailService.deleteSelectedMail(ids);
		return "redirect:/toMailList.do";
	}

	@RequestMapping("/mailModify.do")
	public String modifyMail(MailForm mailForm) {
		Mail mail = new Mail();
		try {
			BeanUtils.copyProperties(mail, mailForm);
			String updateTime = DateFormatUtils.format(new Date(),
					"yyyy.MM.dd HH:mm:ss");
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mailService.modifyMail(mail);
		return "redirect:/toMailList.do";
	}

	@ExceptionHandler({ ControllerException.class })
	public String exception(ControllerException e, HttpServletRequest request) {
		request.setAttribute("error", e.getMessage());
		return "/admin/error/error";
	}
}
